package org.jeecg.modules.base.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.base.entity.BaseStudent;
import org.jeecg.modules.base.entity.vo.StudentVo;
import org.jeecg.modules.base.service.IBaseScoreService;
import org.jeecg.modules.base.service.IBaseStudentService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: base_student
 * @Author: jeecg-boot
 * @Date:   2021-04-14
 * @Version: V1.0
 */
@Api(tags="base_student")
@RestController
@RequestMapping("/base/baseStudent")
@Slf4j
public class BaseStudentController extends JeecgController<BaseStudent, IBaseStudentService> {
	@Autowired
	private IBaseStudentService baseStudentService;
	@Autowired
	private IBaseScoreService baseScoreService;
	@Autowired
	private ISysUserService sysUserService;

	 /**
	  * 学生个人中心
	  * @return
	  */
	@AutoLog(value = "base_student-学生个人中心")
	@ApiOperation(value="base_student-学生个人中心", notes="base_student-学生个人中心")
	@GetMapping(value = "/list")
	public Result<?> queryPageList() {
		LambdaQueryWrapper<BaseStudent> wrapper = new LambdaQueryWrapper();
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		wrapper.eq(BaseStudent::getId,sysUser.getId());
		BaseStudent baseStudent = baseStudentService.getOne(wrapper);
		StudentVo vo = new StudentVo();
		BeanUtils.copyProperties(baseStudent,vo);
		vo.setScore(baseScoreService.getSum(vo.getId()));
		return Result.OK(vo);
	}

	 /**
	  * 管理员学生详情
	  *
	  */
	 @AutoLog(value = "base_student-管理员学生详情")
	 @ApiOperation(value="base_student-管理员学生详情", notes="base_student-管理员学生详情")
	 @GetMapping(value = "/queryList")
	 public Result<?> queryList(BaseStudent baseStudent) {

		 return Result.OK(baseStudentService.getList(baseStudent));
	 }

	 @PostMapping(value = "/register")
	 public Result<?> register(@RequestBody BaseStudent baseStudent) {
	 	LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
	 	wrapper.eq(SysUser::getUsername,baseStudent.getUsername());
	 	List<SysUser> sysUsers = sysUserService.list(wrapper);
	 	if(!CollectionUtils.isEmpty(sysUsers)){
	 		return Result.error("该用户名已存在！");
		}
		 SysUser user = new SysUser();
		 user.setUsername(baseStudent.getUsername());
		 user.setRealname(baseStudent.getRealname());
		 user.setPassword(baseStudent.getPassword());
		 user.setStatus(CommonConstant.USER_UNFREEZE);
		 user.setDelFlag(CommonConstant.DEL_FLAG_0);
		 user.setActivitiSync(CommonConstant.ACT_SYNC_0);
		 sysUserService.addUserWithRole(user,"ee8626f80f7c2619917b6236f3a7f02b");
		 baseStudentService.save(baseStudent);
		 return Result.OK("注册成功！");
	 }
	/**
	 *   添加
	 *
	 * @param baseStudent
	 * @return
	 */
	@AutoLog(value = "base_student-添加")
	@ApiOperation(value="base_student-添加", notes="base_student-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody BaseStudent baseStudent) {
		baseStudentService.save(baseStudent);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param baseStudent
	 * @return
	 */
	@AutoLog(value = "base_student-编辑")
	@ApiOperation(value="base_student-编辑", notes="base_student-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody BaseStudent baseStudent) {
		baseStudentService.updateById(baseStudent);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "base_student-通过id删除")
	@ApiOperation(value="base_student-通过id删除", notes="base_student-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		baseStudentService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "base_student-批量删除")
	@ApiOperation(value="base_student-批量删除", notes="base_student-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.baseStudentService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "base_student-通过id查询")
	@ApiOperation(value="base_student-通过id查询", notes="base_student-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		BaseStudent baseStudent = baseStudentService.getById(id);
		if(baseStudent==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(baseStudent);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param baseStudent
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BaseStudent baseStudent) {
        return super.exportXls(request, baseStudent, BaseStudent.class, "base_student");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BaseStudent.class);
    }

}
